<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" 
	import="java.util.*"
	import="org.jb.sharedbills.shared.dto.*"
	import="org.jb.sharedbills.shared.model.*"
	import="org.jb.sharedbills.shared.model.logic.*"
	import="org.jb.sharedbills.server.JspHelper"%>
<%
/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
EventInfo eventInfo = null;
Event event = null;
String id = request.getParameter("id");
if (id == null || id.isEmpty()) {
	response.sendError(400);
	return;
} else {
	eventInfo = JspHelper.getEventService( pageContext.getServletContext() ).getEventInfo(id);
	if (eventInfo == null) {
		response.sendError(500);
		return;
	} 
	event = eventInfo.getEvent();
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><%= event.getName() %></title>
<link rel="stylesheet" type="text/css" href="report.css" />
</head>
<body>

<table class="members">
	<caption>Balances</caption>
	<tr class="header">
		<th>Nom</th>
		<th>A payé</th>
		<th>Cout</th>
		<th>Balance</th>
	</tr>
<% for( Member m : event.getMembers() ) {%>
	<tr class="<%= m.getBalance() < 0 ? "debit" : "credit"%>">
		<td><%= m.getName() %></td>
		<td class="num"><%= String.format("%0$.2f",m.getMaxAmount()) %></td>
		<td class="num"><%= String.format("%0$.2f",m.getUsed()) %></td>
		<td class="num"><%= String.format("%0$.2f",m.getBalance()) %></td>
	</tr>
<% } %>
</table>

<%  List<Refund> refunds = RefundBuilder.build( event.getMembers() ); %>

<table class="refunds">
	<caption>Proposition de remboursement</caption>
	<tr class="header">
		<th>De</th>
		<th>Vers</th>
		<th>Somme</th>
	</tr>
<%for( Refund refund : refunds ) { %>
	<tr>
		<td><%= refund.getFrom().getName() %></td>
		<td><%= refund.getTo().getName() %></td>
		<td class="num"><%= String.format("%0$.2f",refund.getAmount()) %></td>
	</tr>
<% } %>
</table>

<table class="bills">
	<caption>Dépenses</caption>
	<tr class="header">
		<th>De</th>
		<th>Participants</th>
		<th>Montant</th>
		<th>Description</th>
	</tr>
<%for( Bill bill : event.getBills() ) { %>
	<tr>
		<td><%= bill.getFrom().getName()%></td>
		<td><%= bill.getTo().getName() %></td>
		<td class="num"><%= String.format("%0$.2f",bill.getAmount()) %></td>
		<td><%= bill.getName() == null ? "" : bill.getName() %></td>
	</tr>
<%}%>
</table>

<table class="groups">
	<caption>Groupes</caption>
	<tr class="header">
		<th>Nom</th>
		<th>Participants</th>
	</tr>
<%for(Group group : event.getGroups() ) { %>
	<tr>
		<td><%= group.getName() %></td>
		<td><%= MessageHelper.format( group.getMembers(), false ) %></td>
	</tr>
<%}%>
</table>

</body>
</html>